package com.itheima.dubbo.web;

import com.itheima.dubbo.UserInterface;
import com.itheima.dubbo.pojo.User;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Method;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

@RestController
@RequestMapping("user")
public class UserController {
//    @DubboReference(timeout = 3000)//消费者 接口级别定义超时时间3000
    @DubboReference(
            version = "1.0.1", // 版本号设置，用于灰度发布
            check = false, // 检查订阅服务是否启用，未启用则报错，false则不检查
            methods = {
                @Method(
                    name = "hello",
                    timeout = 1500, //方法级别定义超时时间1500  优先级最高，以下皆为方法级别配置
                    retries = 0  //重试次数，默认为2，非幂等操作（如插入数据）建议设为0
//                    loadbalance = "random", // 负载均衡策略：加权随机  （默认）
//                    loadbalance = "roundrobin", // 负载均衡策略：加权轮询
//                    loadbalance = "leastactive", // 负载均衡策略：最小响应时间
//                    loadbalance = "consistenhash", // 负载均衡策略：一致性哈希

            )
    })
    private UserInterface userInterface;

    @GetMapping("hello")
    public String hello(String name){
        Random random = new Random(100000);
        Integer integer = Integer.valueOf(random.nextInt(100000));
        System.out.println("integer = " + integer);
        User user = new User("zhangsan",1000.0);
        RpcContext.getContext().setAttachment("password", integer);
        RpcContext.getContext().setAttachment("user", user);

        return userInterface.hello(name);
    }
}
